package com.zz.dao;

import com.zz.mapper.AdminMapper;
import com.zz.pojo.Admin;
import com.zz.pojo.Pet;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AdminMen implements AdminMapper {
    //一个连接池管理对象
    private static SqlSessionFactory sqlSessionFactory;

    public static void main(String[] args) throws IOException, ParseException {


        //新建一个类实例对象，执行before中的操作
        AdminMen petdoll = new AdminMen();

        //因为我们需要加载mybatis的配置文件
        String path="mybatis-config.xml";
        //通过IO流 读入配置文件
        InputStream stream = Resources.getResourceAsStream(path);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);



        petdoll.findAll();

        //petdoll.shelfpet("修耗子","鼠鼠我啊","公","01-09-2000","再多看一眼就会爆炸");

        //petdoll.deletepet("鼠鼠我啊");

        //petdoll.updatepetname("小灰","灰风");

        //petdoll.updatepetmaster("步惊云","小灰");

        //petdoll.findVariety("修猫");

        //petdoll.findAllAdmin();

        //petdoll.updateUserName("严凯文","胡图图");

        //petdoll.updatePassword("谢宏伟","114514");

    }

    @Override
    public boolean adminlogin(String username, String password) {

        String tmp= adminloginverification(username);

        if(tmp==null){
            System.out.println("登陆失败！该管理员不存在！");
            return false;
        }
        else {
            if (tmp.equals(password)) {
                System.out.println("登陆成功！！");
                return true;
            } else {
                System.out.println("登陆失败！密码错误！");
                return false;
            }
        }

    }

    public String  adminloginverification(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        String tmp=mapper.adminloginverification(username);

        sqlSession.commit();
        sqlSession.close();
        return tmp;
    }




    //添加新宠物！！！！——————————————————————————————————————————————————————————————————————————————
    @Override
    public void shelfpet(String variety, String petname, String sex, String date, String health) throws ParseException {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);

        String date_string = date;
        //实例化SimpleDateFormat类
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        //将给定的String解析为Date对象
        Date birthday = formatter.parse(date_string);
        mapper.shelfpet(new Pet(variety, petname, sex, birthday, health));

        //提交数据
        sqlSession.commit();
        sqlSession.close();
        System.out.println("添加成功");
    }
    //没啥用，但不能没有
    @Override
    public void shelfpet(Pet pet) {

    }

    //删除宠物信息——————————————————————————————————————————————————————————————————————————————————————————————

    @Override
    public void deletepet(String petname) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        mapper.deletepet(petname);

        sqlSession.commit();
        sqlSession.close();
        System.out.println("删除成功");


    }

    //修改宠物信息————————————————————————————————————————————————————————————————————————————————————————————————

    @Override
    public void updatepetname(String newdata,String petname) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        mapper.updatepetname(newdata,petname);

        sqlSession.commit();
        sqlSession.close();
        System.out.println("修改成功");

    }

    @Override
    public void updatepetmaster(String newdata, String petname) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        mapper.updatepetmaster(newdata,petname);

        sqlSession.commit();
        sqlSession.close();
        System.out.println("修改成功");

    }
    //查询某类宠物信息
    @Override
    public List<Pet> findVariety(String variety) {
        //使用sqlSessionFactory 创建一个sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);

        List<Pet> list = mapper.findVariety(variety);
        for (Pet pet:list){
            System.out.println(pet);
        }
        sqlSession.close();
        return null;
    }

    @Override
    public List<Pet> findAll() {
        //使用sqlSessionFactory 创建一个sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);

        List<Pet> list = mapper.findAll();
        for (Pet pet:list){
            System.out.println(pet);
        }
        sqlSession.close();
        return null;
    }
    //修改管理员名称
    @Override
    public void updateUserName(String username, String newname) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        mapper.updateUserName(newname,username);

        sqlSession.commit();
        sqlSession.close();
        System.out.println("修改成功");
    }
    //修改管理员密码
    @Override
    public void updatePassword(String username, String newpassword) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        mapper.updatePassword(newpassword,username);

        sqlSession.commit();
        sqlSession.close();
        System.out.println("修改成功");
    }



    //查询管理员列表+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    @Override
    public List<Admin> findAllAdmin() {
        //使用sqlSessionFactory 创建一个sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);

        List<Admin> list = mapper.findAllAdmin();
        for (Admin admin:list){
            System.out.println(admin);
        }
        sqlSession.close();
        return list;
    }

}
